--
-- Created by IntelliJ IDEA.
-- User: wzq
-- Date: 16-10-9
-- Time: 上午12:00
-- To change this template use File | Settings | File Templates.
--

local hex_to_char = function(x)
  return string.char(tonumber(x, 16))
end

local unescape = function(url)
  return url:gsub("%%(%x%x)", hex_to_char)
end

function parseDubbo(dubboStr)
  return unescape(dubboStr)
end

function parseDubboRest(dubboStr)
  assert(type(dubboStr) == 'string', 'dubboStr not string')
  local url = unescape(dubboStr)
  --  local ip, port, type, service = string.match(url, 'rest://([\d\.]+)(:\d+)?/(\a+)/([\a\.]+)?.*')
  --  local ip, port = string.match(url, 'rest://(.+):.*')
  local p1, p2, p3, p4 = string.match(url, 'rest://(.-:.-)/(.-)/(.-)?.*')
  if p1 then print(p1) end
  if p2 then print(p2) end
  if p3 then print(p3) end
  if p4 then print(p4) end
  --  if not ip then return nil end
  --  return { ip = ip, port = port, type = type, service = service }
end

local url = "rest%3A%2F%2F107.170.197.191%3A8088%2Fab-master%2Fcom.foreveross.service.ApprovalRestService%3Fanyhost%3Dtrue%26application%3Dab-sample-master%26connections%3D200%26dubbo%3D2.8.4%26generic%3Dfalse%26group%3DxmlConfig%26interface%3Dcom.foreveross.service.ApprovalRestService%26logger%3Dslf4j%26methods%3DaddApproval%2CmodifyApprovalById%2Ccheck%2CgetApprovalList%26organization%3Dwww.foreveross.com%26owner%3DShinE%26pid%3D1%26revision%3D1.0%26server%3Dservlet%26side%3Dprovider%26timestamp%3D1475332995445%26validation%3Dtrue"


print(parseDubbo(url))

local result = parseDubboRest(url)
--local result = parseDubboRest2(url)
--if result then
--  for k, v in pairs(result) do
--    print(k .. '=' .. v)
--  end
--else
--  print("nil result")
--end